<template>
  <div>
    <div class="DvPad">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
                    <el-form-item label="cron执行表达式" prop="cronExpression">
                      <el-input
                          v-model="queryParams.cronExpression"
                          placeholder="请输入cron执行表达式"
                          clearable
                          @keyup.enter.native="handleQuery"
                      />
                    </el-form-item>
                    <el-form-item label="计划执行错误策略" prop="misfirePolicy">
                      <el-input
                          v-model="queryParams.misfirePolicy"
                          placeholder="请输入计划执行错误策略"
                          clearable
                          @keyup.enter.native="handleQuery"
                      />
                    </el-form-item>
                    <el-form-item label="是否并发执行" prop="concurrent">
                      <el-input
                          v-model="queryParams.concurrent"
                          placeholder="请输入是否并发执行"
                          clearable
                          @keyup.enter.native="handleQuery"
                      />
                    </el-form-item>
        <el-form-item>
          <Btn text="查询" size="small" @dian="handleQuery"/>
          <Btn text="重置" type="border-1" size="small" @dian="resetQuery"/>
        </el-form-item>
      </el-form>
    </div>

    <div class="DvPad lz-margin-top-20">
      <div class="buttonDv lz-margin-bottom-20">
        <Btn text="新增" @dian="handleAdd" v-hasPermi="['system:job:add']"/>
        <Btn
            text="禁用"
            type="border-1"
            :disabled="disable"
            @dian="handleDisable"
        />
        <Btn
            text="激活"
            type="border-1"
            :disabled="disable"
            @dian="handleActivate"
        />
        <Btn text="导入" type="border-1" @dian="handleImport" v-hasPermi="['system:job:add']"/>
        <Btn
            text="导出"
            type="border-1"
            @dian="handleExport"
            v-hasPermi="['system:job:export']"
        />
      </div>
    </div>

    <el-table v-loading="loading" :data="jobList" style="min-height: 400px"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" :index="index" width="50"/>
              <el-table-column label="任务ID" align="center" prop="jobId"/>
              <el-table-column label="任务名称" align="center" prop="jobName"/>
              <el-table-column label="任务组名" align="center" prop="jobGroup"/>
              <el-table-column label="调用目标字符串" align="center" prop="invokeTarget"/>
              <el-table-column label="cron执行表达式" align="center" prop="cronExpression"/>
              <el-table-column label="计划执行错误策略" align="center" prop="misfirePolicy"/>
              <el-table-column label="是否并发执行" align="center" prop="concurrent"/>
              <el-table-column label="状态" align="center" prop="status"/>
              <el-table-column label="备注信息" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              v-hasPermi="['system:job:query']"
              @click="handleDetails(scope.row)"
          >查看
          </el-button>
          <el-divider direction="vertical" v-hasPermi="['system:job:query']"></el-divider>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:job:edit']"
          >修改
          </el-button>
          <el-divider direction="vertical" v-hasPermi="['system:job:edit']"></el-divider>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:job:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <Dialog v-bind="dialogData" @close="DialogClose" @update="DialogUpdate"/>

    <pagination
        align="center"
        v-show="total>0"
        :total="total"
        layout="prev, pager, next"
        next-text="下一页"
        prev-text="上一页"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加页面 -->
    <div>
      <!-- 页面返回 -->
      <ViewsHead></ViewsHead>
      <el-card class="lz-padding-top-20">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row :gutter="100">
            <el-col :span="8">
              <el-row :gutter="100">
                          <el-col :span="12">
                                <el-form-item label="调用目标字符串" prop="invokeTarget">
                                  <el-input v-model="form.invokeTarget" type="textarea" placeholder="请输入内容"/>
                                </el-form-item>
                          </el-col>
                          <el-col :span="12">
                                <el-form-item label="cron执行表达式" prop="cronExpression">
                                  <el-input v-model="form.cronExpression" placeholder="请输入cron执行表达式"/>
                                </el-form-item>
                          </el-col>
                          <el-col :span="12">
                                <el-form-item label="计划执行错误策略" prop="misfirePolicy">
                                  <el-input v-model="form.misfirePolicy" placeholder="请输入计划执行错误策略"/>
                                </el-form-item>
                          </el-col>
                          <el-col :span="12">
                                <el-form-item label="是否并发执行" prop="concurrent">
                                  <el-input v-model="form.concurrent" placeholder="请输入是否并发执行"/>
                                </el-form-item>
                          </el-col>
                          <el-col :span="12">
                          </el-col>
                          <el-col :span="12">
                                <el-form-item label="备注信息" prop="remark">
                                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
                                </el-form-item>
                          </el-col>

              </el-row>
            </el-col>
            <el-col :span="16" style="height: 58.5px">
              <el-row>
                <el-col :span="24">
                  <el-form-item label="地图位置" prop="picture">
                    <AMap />
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="设备图片" prop="picture">
                    <image-upload v-model="form.picture" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-form>
        <div class="lz-margin-top-80 lz-margin-left-50 lz-padding-top-50">
          <Btn text="确定" @dian="submitForm('ruleForm')" />
          <Btn text="取消" type="border-2" @dian="backPage" />
        </div>
      </el-card>
    </div>

  </div>
</template>

<script>
  import Btn from "@/views/components/button/index";


  export default {
    name: "Job",
    components: {Btn, Dialog , AMap},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 定时任务调度表格数据
              jobList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
                        invokeTarget: null,
                        cronExpression: null,
                        misfirePolicy: null,
                        concurrent: null,
                        status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
                        invokeTarget: [
                    {
                      required: true, message: "调用目标字符串不能为空", trigger: "blur" }
                  ],
        },
        // 封装弹框
        dialogData: {
          id: null,
          dialogVisible: false,
          title: "",
          content: "",
        },
        disable: true,
      };
    },
    created() {
      this.getList();
    },
    methods: {
      index(index) {
        return (
            (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
        );
      },
      /** 遮罩层事件 */
      DialogUpdate(data) {
        console.log(data);
        this.dialogData = {
          id: null,
          dialogVisible: false,
          title: "",
          content: "",
        };
      },
      // 关闭封装遮罩层
      DialogClose() {
        this.dialogData = {
          id: null,
          dialogVisible: false,
          title: "",
          content: "",
        };
        this.getList();
      },
      /** 查询定时任务调度列表 */
      getList() {
        this.loading = true;
        listJob(this.queryParams).then(response => {
          this.jobList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
                        jobId: null,
                        jobName: null,
                        jobGroup: null,
                        invokeTarget: null,
                        cronExpression: null,
                        misfirePolicy: null,
                        concurrent: null,
                        status: null,
                        createBy: null,
                        createTime: null,
                        updateBy: null,
                        updateTime: null,
                        remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.jobId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加定时任务调度";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const jobId = row.jobId || this.ids
        getJob(jobId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改定时任务调度";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.jobId != null) {
              updateJob(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addJob(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const jobIds = row.jobId || this.ids;
        this.$modal.confirm('是否确认删除定时任务调度编号为"' + jobIds + '"的数据项？').then(function() {
          return delJob(jobIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
  /** 导出按钮操作 */
  handleExport() {
    this.download('system/job/export', {
      ...this.queryParams
    }, `job_${new Date().getTime()}.xlsx`)
  },
  /* 查看详情操作 */
  handleDetails(row) {
              this.$router.push("${businessName}View?=row.id" );
  },
  /** 修改按钮操作 */
  handleUpdate(row) {
          this.$router.push("${businessName}Add?=row.id");
  },
  /** 导入按钮操作 */
  handleImport() {
    this.dialogData = {
      dialogVisible: true,
      title:  "定时任务调度数据导入",
      importTiele: "定时任务调度信息",
      FILETYPE: 18
    };
  },
  }
  }
  ;
</script>
<style lang="scss" scoped>
  .el-button {
    color: #5c6ffe;
  }

  .DvPad {
    width: 100%;
    padding: 20px;
    background: white;
    border-radius: 8px;
    box-shadow: 0px 2px 8px 0px rgba(143, 155, 187, 0.15);

    .fontFun {
      margin-left: 20px;
      color: #5a5a89;
      font-size: 12px;
    }

    .buttonDv {
      width: 100%;
    }
  }
</style>
<style lang="scss" scoped>
  el-col {
    height: 58.5px;
  }
  .el-select {
    width: 100%;
  }
  ::v-deep .el-input-group__append {
    width: 20px;
  }
</style>
